#!/usr/bin/python
# Created by Juan C. Espinoza <jucar.espinoza@gmail.com> October 05, 2009

import os, gzip, shutil, traceback
from lisn_utils import *
import argparse

FTP_inpe = MyFTP('10.10.16.1', 'lisnRT', 'yT495i8L')
zip_cmd = '/usr/bin/compress'

htnk = Hatanaka()
request = LISNRequest(debug=False)
params = request.params

def is_gps_file(filename, binary=False):
    '''
    '''
    for label in gps_names_new:
        if label in filename.split('/')[-1]:
            if binary:
                if gps_names_new[label][2] in ('binary', 'rinex'):
                    return True
                else:
                    return False
            else:
                return True
    return False

def is_rnx_file(filename):
    '''
    '''
    for label in rnx_names:
        if label in filename.split('/')[-1]:
            return True
    return False

def get_site(filename, sites):
    '''
    '''
    dum = [s['lisn_code'] in filename for s in sites]
    if True in dum:
        return sites[dum.index(True)]
    else:
        return False
    

def copy_file(file_old, path_new, station, file_date, gz=False, movefile=False):
    '''
    '''
    
    file_new = ''
    for label, vars in gps_names_new.items():
        if label in file_old.split('/')[-1]:
            file_new = vars[0] % (station['code'], file_date.strftime('%y%m%d_%H%M'))
            break
    if not file_new:
        print 'Unknown file type: %s', file_old
        return False
         
    if not os.path.exists(path_new):
        os.makedirs(path_new)

    file_new = os.path.join(path_new, file_new)

    #print 'Copying file: %s to %s' % (file_old, file_new)
    shutil.copy(file_old, file_new)

    
    path = os.path.dirname(file_old)
    #file_date = GPSDateTime(dt_str)
    fileout = os.path.join(path, '%s.%s.%s' % (station['lisn_code'], file_date.strftime('%y%m%d_%H%M'), 's4'))
    rnx_name = fileout.replace('.s4', '')
    rnx = [False]
    kwargs = {}
    kwargs['xyz'] = gps.lla2xyz(*station['location'])
    kwargs['comment'] = 'Location: %s\n' % station['fullname']
    try:
        if 's4' in file_new:       
            cat = cat_files(fileout, [file_old], file_date.date(), 30, 's4', 's4.gz', daily=False)
            if cat:
                os.system('%s -f %s' % (zip_cmd, fileout))
                fileout += '.Z'
                FTP_inpe.upload(fileout, fileout.split('/')[-1])
                os.remove(fileout)
        elif 'nvd' in file_old:
            rnx = nvd_to_rnx(file_old, station['code'], path,
                                         date=file_date.date(), interval=30,
                                         alt_name=rnx_name, **kwargs)
        elif 'lb2' in file_old:
            rnx = lb2_to_rnx(file_old, station['code'], path,
                                         date=file_date.date(), interval=30,
                                         alt_name=rnx_name, **kwargs)
        elif 'obs' in file_old:
            rnx = obs_to_rnx(file_old, station['code'], path,
                                         date=file_date.date(), interval=30,
                                         alt_name=rnx_name, **kwargs)
        if rnx[0]:
            zrnx = htnk.compress(rnx[0], True, True)
            FTP_inpe.upload(zrnx, zrnx.split('/')[-1])
            os.remove(zrnx)
    except:
        print 'Error processing: %s' % file_old   
    
    #if movefile:
    #    print 'Deleting file: ' + file_old
    #    os.remove(file_old)
    
    return True

def get_files(vars):
    '''
    Get raw gps data (Novatel, Leica and Ashtech) from servers,
    add datetime info to the filename and copy to database, also update
    mysql.
    '''
    
    print 'Start at ' + GPSDateTime.now().ctime()
    #create folders
    if not os.path.exists(vars.output):
        os.makedirs(vars.output)
    if not os.path.exists(vars.temp):
        os.makedirs(vars.temp)    

    for server in params.gps_server():
        [host, username, password, path, sftp] = params.gps_server(server)
        if server=='montecillo':
            FTP = MyFTP(host, username, password, sftp)
        else:
            FTP = MyFTP(host, username, password, sftp, True)
        if not FTP.error:
            sites = request.stations(instrument='gps', server=server)
            if vars.sites:
                sites  = [s for s in sites if s['code'] in vars.sites.split(',')]
            file_list = FTP.listdir(path)
            file_list.sort()
            for remote_file in file_list:
                kwargs = {}
                station = get_site(remote_file, sites)
                if station is False or is_gps_file(remote_file) is False:
                    print 'Unknown file: %s' % remote_file
                    continue
                #try:
                new_time = FTP.utime(os.path.join(path, remote_file))
                if station['last_file_date'] in (False, None, '', ' '):
                    old_time = 0
                else:
                    old_time = GPSDateTime(station['last_file_date']).timestamp
                if int(new_time)>int(old_time):
                    FTP.download(path+remote_file, vars.temp+remote_file)
                    file_date = GPSDateTime(int(new_time))
                    old_file = os.path.join(vars.temp, remote_file)
                    new_path = os.path.join(vars.output, file_date.strftime('%Y'), 
                                            station['code'])
                    ret = copy_file(old_file, new_path, station, file_date)
                    kwargs['last_file_date'] = file_date.strftime('%y%m%d_%H%M%S')
                    kwargs['last_file'] = remote_file.lower()
                    kwargs['status'] = 1
                else:
                    kwargs['status'] = 2
                    ret = False    
                    print 'No new file for %s' % remote_file                    
                    
                if ret and is_gps_file(remote_file, True):
                    req = request.update_station(station['code'], 'gps', **kwargs)
                    if req['error']:
                        print 'Error updating station:%s status' % site['code']
                            
          #      except:
          #          print 'Error downloading file: %s ' % (path+remote_file)
          #          traceback.print_exc(2)
        FTP.close()
     
    print 'Finish at ' + GPSDateTime.now().ctime()
    
def main():
    '''
    Parse arguments to program.
    '''

    parser = argparse.ArgumentParser(description='Download gps files from montecillo & capricorn2')    
    # add options and arguments  
    parser.add_argument('-s', action='store', dest='sites',  
                        help='stations four-char codes')
    parser.add_argument('-o', action='store', dest='output', default='/data1/lisn/',
                        help='Output path for save files')
    parser.add_argument('-t', action='store', dest='temp', default='/data/temp/gps/',
                        help='Path for temporary files')
    
    args = parser.parse_args()
    get_files(args)
        
#Execute main routine
if __name__ == "__main__":
    main()
    #FTP_inpe.close()